var embed_div;
var previous_embed;
var myEmbood;
YAHOO.util.Event.onDOMReady(initfun);
function initfun(){
	embed_div = document.getElementById('embedcontainer');
	document.getElementById("id_link").onkeyup =function() {makeRequest(document.getElementById("id_link").value); };
	document.getElementById("emboodle_close").onclick= function() {closeEmboodle();};
	makeRequest(document.getElementById("id_link").value);
	//^kind of a hack to make edit emboodle populate when the user clicks edit
	//really we should be displaying the embed code.
}

var closeEmboodle = function() {
	//document.getElementById('id_link').style.display='';
	document.getElementById('id_link').value='';
	//TODO: also show the link label
	document.getElementById('uber_container').style.display='none';
}

var handleSuccess = function(o){

	YAHOO.log("The success handler was called.  tId: " + o.tId + ".", "info", "example");
	if(o.responseText == undefined) {
		
	}
	else{
		
		description='';
		title='';
	
		try {
			myEmbood = null;
			myEmbood = YAHOO.lang.JSON.parse(o.responseText);
			embedcode = myEmbood.embedcode;
			title=myEmbood.title;
			description=myEmbood.description;
		}
		catch (err) {
			//embed_div.innerHTML = 'JSON failed: '+ err + '<br/>tt' + o.responseText;
			embedcode = 'No embeddable object found.';
			//document.getElementById('uber_container').style.display='';
 			//document.getElementById('emboodle_container').style.display='';
 			//document.getElementById('emboodle_loading').style.display='none';
 			return;
		}
		finally {
			embed_div.innerHTML = embedcode;
			document.getElementById("emboodle_title").innerHTML=title;
			document.getElementById("authordesccontainer").innerHTML=description;
			
			//These are to store in a hidden state to submit to DB
			document.getElementById("id_embedcode").setAttribute('value',embedcode);
			document.getElementById("id_name").setAttribute('value',title);
			document.getElementById("id_authordesc").setAttribute('value',description);
			
			//Now display it all
			document.getElementById('uber_container').style.display='';
 			document.getElementById('emboodle_container').style.display='';
 			document.getElementById('emboodle_loading').style.display='none';
	 	}
		
		
	}
}

var handleFailure = function(o){

		YAHOO.log("The failure handler was called.  tId: " + o.tId + ".", "info", "example");

	if(o.responseText !== undefined){
		embed_div.innerHTML = "<ul><li>Transaction id: " + o.tId + "</li>";
		embed_div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
		embed_div.innerHTML += "<li>Status code message: " + o.statusText + "</li></ul>";
	}
}

var callback =
{
  success:handleSuccess,
  failure:handleFailure,
  argument: { foo:"foo", bar:"bar" }
};

var sUrl = "../mod/emboodle/emboodfetcher.php";

function makeRequest(url){
	if(!isUrl(url)) {
		return;
	}
	

	//document.getElementById('id_link').style.display='none';
	//TODO: also hide link's label
 	document.getElementById('uber_container').style.display='';
 	document.getElementById('emboodle_container').style.display='none';
 	document.getElementById('emboodle_loading').style.display='';
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl + "?url=" + url, callback);
	
	YAHOO.log("Initiating request; tId: " + request.tId + ".", "info", "example");
	//document.getElementById("id_link").setAttribute('value',url);

}

function isUrl(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}


